# -*- coding: utf-8 -*-
"""
Created on Tue Jun 10 17:41:34 2025

@author: micha
"""

import csv
import os

def split_csv(input_file, output_dir, rows_per_file=1000, keep_headers=True):
    """Splits a CSV file into multiple smaller files.

    Args:
        input_file (str): Path to the input CSV file.
        output_dir (str): Directory to store the split files.
        rows_per_file (int): Number of rows per split file.
        keep_headers (bool): Whether to include the header row in each split file.
    """
    os.makedirs(output_dir, exist_ok=True)
    with open(input_file, 'r', newline='', encoding='utf-8') as infile:
        reader = csv.reader(infile)
        if keep_headers:
            header = next(reader)
        file_num = 1
        row_count = 0
        outfile = None
        writer = None
        for row in reader:
            if row_count == 0:
                output_file = os.path.join(output_dir, f'output_{file_num}.csv')
                outfile = open(output_file, 'w', newline='', encoding='utf-8')
                writer = csv.writer(outfile)
                if keep_headers:
                    writer.writerow(header)
            writer.writerow(row)
            row_count += 1
            if row_count >= rows_per_file:
                outfile.close()
                file_num += 1
                row_count = 0
        if outfile:
            outfile.close()

# Example usage:
input_csv = "C:/Michael/Albany LAIO Lab and Research Projects/Is a profile worth/data/Cumulative monte carlo.csv"
output_directory = "C:/Michael/Albany LAIO Lab and Research Projects/Is a profile worth/data/CSV Splits"
split_csv(input_csv, output_directory, rows_per_file=500000)